/*
 *  \author Marcin Krzewski
 *  \author Katarzyna Rusinowska 
 *  \author Jakub Sitnicki   
 *  \date    06/2009
 */
#include "Command.hpp"

namespace ged {

	FlipUpDownCommand:: FlipUpDownCommand(){}

	FlipUpDownCommand:: ~FlipUpDownCommand(){}

	void FlipUpDownCommand::execute (const PImage im){
		
		PImageLayer b= im->getLayer();
		b->setView(flipped_up_down_view (b->getView()));
	}

	void FlipUpDownCommand::unExecute(const PImage im) {
			
		PImageLayer b = im->getLayer();
		b->setView(flipped_up_down_view (b->getView()));
	}

	FlipLeftRightCommand:: FlipLeftRightCommand(){}

	FlipLeftRightCommand:: ~FlipLeftRightCommand(){}

	void FlipLeftRightCommand::execute (const PImage im){
		
		PImageLayer b = im->getLayer();
		b->setView(flipped_left_right_view (b->getView()));				
	}

	void FlipLeftRightCommand::unExecute(const PImage im) {
			
		PImageLayer b = im->getLayer();
		b->setView(flipped_left_right_view (b->getView()));		
	}

	SubimageCommand::SubimageCommand(int x_m ,int y_m, int w, int h):x_min_(x_m),y_min_(y_m),width_(w),height_(h){}

	SubimageCommand::~SubimageCommand(){}

	void SubimageCommand::execute(const PImage im){
		
		int w_m= im ->getLayer()->getView().width();
		int h_m= im ->getLayer()->getView().height();

		if(x_min_+width_ >w_m)
			width_=w_m-x_min_;
		if(y_min_+height_ >h_m)
			height_=h_m-x_min_;
	
		PImageLayer b = im->getLayer();
		img_lay_view_ = b->getView();
		b->setView(subimage_view(b->getView(),x_min_, y_min_, width_, height_ ) );
	}	

	void SubimageCommand:: unExecute(const PImage im) {
			
		PImageLayer b = im->getLayer();
		b->setView(img_lay_view_);	
	}

	SubsampleCommand::SubsampleCommand(int x, int y):x_step_(x),y_step_(y){}

	SubsampleCommand::~SubsampleCommand(){}

	void SubsampleCommand:: execute(const PImage im){
		
		PImageLayer b = im->getLayer();
		img_lay_view_ = b->getView();
		b->setView(subsampled_view(b->getView(),x_step_, y_step_ ) );	
	}

	void  SubsampleCommand::unExecute(const PImage im){
		
		PImageLayer b = im->getLayer();
		b->setView(img_lay_view_);	
	}

	RotateCwCommand:: RotateCwCommand(){}

	RotateCwCommand:: ~RotateCwCommand(){}

	void RotateCwCommand:: execute(const PImage im){
		
		PImageLayer b = im->getLayer();
		b->setView(rotated90cw_view (b->getView()));
	}	

	void RotateCwCommand:: unExecute(const PImage im) {
		
		PImageLayer b = im->getLayer();
		b->setView(rotated90ccw_view (b->getView()));
	}

	RotateCcwCommand:: RotateCcwCommand(){}

	RotateCcwCommand:: ~RotateCcwCommand(){}

	void RotateCcwCommand:: execute(const PImage im){
		
		PImageLayer b = im->getLayer();
		b->setView(rotated90ccw_view (b->getView()));	
	}	

	void RotateCcwCommand:: unExecute(const PImage im) {
		
		PImageLayer b = im->getLayer();
		b->setView(rotated90cw_view (b->getView()));		
	}

	SelectLayerAboveCommand:: SelectLayerAboveCommand(){chk_=true;}
	SelectLayerAboveCommand:: ~SelectLayerAboveCommand(){}

	void SelectLayerAboveCommand:: execute(const PImage im){
	try{
		im->selectLayer(im->getActiveLayer()+1);}
	catch(std:: out_of_range &e)
		{
		chk_= false;}
	}

	void SelectLayerAboveCommand:: unExecute(const PImage im){
		
		if(chk_!=false)
		im->selectLayer(im->getActiveLayer()-1);	
	}

	SelectLayerBelowCommand:: SelectLayerBelowCommand(){chk_=true;}
	SelectLayerBelowCommand:: ~SelectLayerBelowCommand(){}

	void SelectLayerBelowCommand:: execute(const PImage im){
	try{
		im-> selectLayer(im->getActiveLayer()-1);}
	catch(std:: out_of_range &e)
		{
		chk_= false;}
	}

	void SelectLayerBelowCommand:: unExecute(const PImage im){
		
		if(chk_!=false)
		im -> selectLayer(im->getActiveLayer()+1);	
	}

	MoveLayerCommand:: MoveLayerCommand(int x, int y):offset_(GILPoint(x,y)),x_(x),y_(y){}
	MoveLayerCommand:: ~MoveLayerCommand(){}

	void MoveLayerCommand:: execute(const PImage im){
		
		PImageLayer b = im->getLayer();
		oldOffset_= b->getOffset();
		b->setOffset(offset_);
	}

	void MoveLayerCommand:: unExecute(const PImage im){
		
		PImageLayer b = im->getLayer();
		b->setOffset(GILPoint(oldOffset_.x,oldOffset_.y));
		
	}

}
